package com.nondo.dean.platform.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Grade;
import com.nondo.dean.platform.domain.GradeExample;
import com.nondo.dean.platform.mapper.GradeMapper;

/**
 * 等级管理业务类
 * 
 * @author zhaoljin
 * @date 2018年3月27日
 */
@Component
public class GradeService {

    @Autowired
    private GradeMapper gradeMapper;

    /**
     * 插入等级
     * @param grade
     * @return
     */
    @Transactional
    public int addGrade(Grade grade) {
        return gradeMapper.insertSelective(grade);
    }

    /**
     * 修改等级
     * @param grade
     * @return
     */
    @Transactional
    public int updateGrade(Grade grade) {
        return gradeMapper.updateByPrimaryKeySelective(grade);
    }

    /**
     * 按照主键ID查询等级
     * @param id
     * @return
     */
    public Grade selectGrade(Integer id) {
        return gradeMapper.selectByPrimaryKey(id);
    }

    /**
     * 等级分页查询列表
     * @param offset
     * @param limit
     * @param grade
     * @return
     */
    public List<Grade> getGradeList(int offset, int limit, Grade grade) {
        GradeExample example = new GradeExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        GradeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, grade);
        return gradeMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 等级总数统计
     * @param grade
     * @return
     */
    public Long count(Grade grade) {
        GradeExample example = new GradeExample();
        GradeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, grade);
        return gradeMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param grade
     */
    void setCriteria(GradeExample.Criteria criteria, Grade grade) {
        if (grade != null) {
            if (grade.getId() != null)
                criteria.andIdEqualTo(grade.getId());
            if (StringUtils.isNoneBlank(grade.getName()))
                criteria.andNameLike("%" + grade.getName() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

   /**
    * 按照等级名称查询等级
    * @param name
    * @return
    */
    public Grade selectGradeByName(String name) {
        GradeExample example = new GradeExample();
        GradeExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        List<Grade> gradeList = gradeMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(gradeList)?gradeList.get(0):null;
    }
    
    /**
     * 等级列表
     * @return 不分页的等级列表
     */
    public List<Grade> getGradeList() {
        GradeExample example = new GradeExample();
        example.setOrderByClause("create_date DESC");
        GradeExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(String.valueOf(0));
        return gradeMapper.selectByExample(example);
    }
}
